﻿package core.ui.controls
{
    import core.event.*;
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;

    public class NumberInput extends Sprite
    {
        public var t:TextField;
        private var w:int;
        private var h:int;
        private var min:Number;
        private var max:Number;
        private var decimal:Boolean;
        var disable:Sprite;

        public function NumberInput(param1:int, param2:Number, param3:Number = 0, param4:Number = 100, param5:Boolean = false, param6:Boolean = true, param7:int = 20)
        {
            this.min = param3;
            this.max = param4;
            this.decimal = param5;
            this.w = param1;
            this.h = param7;
            if (param6)
            {
                this.graphics.beginFill(9474192);
                this.graphics.drawRect(0, 0, param1, param7);
                this.graphics.beginBitmapFill(new InputBackground());
                this.graphics.drawRect(1, 1, param1 - 2, param7 - 2);
                this.graphics.endFill();
            }
            this.t = new TextField();
            this.t.x = 2;
            this.t.y = 2;
            this.t.defaultTextFormat = new TextFormat("Verdana, Helvetica, San-serif", 12, 3355443);
            this.t.type = TextFieldType.INPUT;
            this.t.width = param1 - 4;
            this.t.height = param7 - 4;
            this.t.maxChars = param4.toString().length;
            if (param3 < 0)
            {
                (this.t.maxChars + 1);
            }
            if (param5)
            {
                this.t.maxChars = this.t.maxChars + 2;
            }
            this.t.restrict = "0-9 +-.";
            if (!param5)
            {
                this.t.text = param2.toString();
            }
            else
            {
                this.t.text = param2.toString() + ".0";
            }
            this.t.addEventListener(KeyboardEvent.KEY_UP, this.Changed, false, 1, true);
            this.t.addEventListener(FocusEvent.FOCUS_OUT, this.FocusOut, false, 1, true);
            this.t.addEventListener(FocusEvent.FOCUS_IN, this.FocusIn, false, 1, true);
            addChild(this.t);
            return;
        }// end function

        public function Disable(addEventListener:Boolean = true) : void
        {
            if (this.disable == null)
            {
                this.disable = new Sprite();
                this.disable.visible = false;
                this.disable.alpha = 0.3;
                this.disable.graphics.beginFill(9474192);
                this.disable.graphics.drawRect(0, 0, 10, 10);
                this.disable.graphics.endFill();
                this.disable.height = this.height;
                this.disable.width = this.width;
                addChild(this.disable);
            }
            this.disable.visible = addEventListener;
            if (addEventListener)
            {
                this.t.selectable = false;
                this.t.type = TextFieldType.DYNAMIC;
            }
            else
            {
                this.t.selectable = true;
                this.t.type = TextFieldType.INPUT;
            }
            return;
        }// end function

        private function Changed(event:KeyboardEvent) : void
        {
            var _loc_2:* = Number(this.t.text);
            if (!this.decimal)
            {
                _loc_2 = Math.round(_loc_2);
            }
            if (_loc_2 < this.min)
            {
                _loc_2 = this.min;
            }
            else if (_loc_2 > this.max)
            {
                _loc_2 = this.max;
            }
            dispatchEvent(new NumberEvent(_loc_2));
            return;
        }// end function

        private function FocusOut(... args) : void
        {
            AppState.TextInteraction = false;
            this.ValidateData();
            return;
        }// end function

        private function FocusIn(event:FocusEvent) : void
        {
            AppState.TextInteraction = true;
            return;
        }// end function

        public function get value() : Number
        {
            return Number(this.t.text);
        }// end function

        public function set value(addEventListener:Number) : void
        {
            this.t.text = addEventListener.toString();
            this.ValidateData();
            return;
        }// end function

        private function ValidateData() : void
        {
            var _loc_1:Number = NaN;
            if (this.t.text == "")
            {
                _loc_1 = this.min;
            }
            else if (Number(this.t.text) < this.min)
            {
                _loc_1 = this.min;
            }
            else if (Number(this.t.text) > this.max)
            {
                _loc_1 = this.max;
            }
            else
            {
                _loc_1 = Number(this.t.text);
            }
            if (_loc_1.toString().indexOf(".") != -1 || !this.decimal)
            {
                this.t.text = Math.round(_loc_1).toString();
            }
            else
            {
                this.t.text = _loc_1.toString() + ".0";
            }
            return;
        }// end function

    }
}
